package com.oa.util.control;

import java.sql.*;

public class FormDataSource
{

    private Statement m_stmt;
    private ResultSet m_rset;

    public FormDataSource()
    {
        m_stmt = null;
        m_rset = null;
    }

    public void initFormDataSource(Connection conn)
        throws Exception
    {
        try
        {
            m_stmt = conn.createStatement();
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
    }

    public void beginSelect(String fieldName1, String fieldName2, String tableName)
        throws Exception
    {
        String sql = String.valueOf(String.valueOf((new StringBuffer("select ")).append(fieldName1).append(",").append(fieldName2).append(" from ").append(tableName)));
        try
        {
            m_rset = m_stmt.executeQuery(sql);
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
    }

    public void beginSelect(String fieldName1, String tableName)
        throws Exception
    {
        String sql = String.valueOf(String.valueOf((new StringBuffer("select distinct ")).append(fieldName1).append(" from ").append(tableName)));
        try
        {
            m_rset = m_stmt.executeQuery(sql);
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
    }

    public boolean hasNextDataInfo()
        throws Exception
    {
        try
        {
            if(m_rset.next())
            {
                boolean flag = true;
                return flag;
            } else
            {
                m_stmt.close();
                boolean flag1 = false;
                return flag1;
            }
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
    }

    public String getOptionId()
        throws Exception
    {
        String optionId = null;
        try
        {
            optionId = m_rset.getString(1);
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
        return optionId;
    }

    public String getOptionValue()
        throws Exception
    {
        String optionValue = null;
        try
        {
            optionValue = m_rset.getString(2);
        }
        catch(Exception e)
        {
            if(m_rset != null)
            {
                m_rset.close();
            }
            if(m_stmt != null)
            {
                m_stmt.close();
            }
            throw e;
        }
        return optionValue;
    }
}
